//
//  XMLReadCaretakers.m
//  Reads XML of caretakers
//
//  <Caretaker>
//  <name>      </name>
//  <email>     </email>
//  <status>    </status>
//  </Caretaker>
//
//  Created by student on 3/8/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "XMLReadCaretakers.h"

@implementation XMLReadCaretakers

@synthesize caretaker;
@synthesize allCaretakers;
@synthesize currentElementValue;

    typedef enum {
        blank,
        caretaker,
        name,
        email,
        link_status
    } CurrentElement;
    
    CurrentElement current_state = blank;
    BOOL insideCaretaker = NO;

    static NSString *xCaretaker = @"caretaker",
                *xName = @"name",
                *xEmail = @"email",
                *xstatus = @"status";

    NSMutableString *xml;

- (NSMutableString *)xml
{
    return xml;
}

// initalize things
- (id) init{
     xml = [[NSMutableString alloc] init ];
    if (self = [super init]){
        caretaker = [[LinkedAccount alloc] init];
        allCaretakers = [[NSMutableArray alloc] init];
        currentElementValue = [[NSMutableString alloc] init ];
    }
    return self;
}


#pragma mark -
#pragma mark NSXMLParserDelegate methods


// enters <element>
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    
    if ([elementName isEqualToString:xCaretaker])
    {
        //NSLog(@"Entering caretaker");
        insideCaretaker = YES;
    }
    else if ([elementName isEqualToString:xName] && insideCaretaker) // inside <name>
    {
        current_state = name;
    }
    else if ([elementName isEqualToString:xEmail] && insideCaretaker) // inside <email>
    {
        current_state = email;
    }
    else if ([elementName isEqualToString:xstatus] && insideCaretaker) // inside <status>
    {
        current_state = link_status;
    }
}

// exits <element>
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
 qualifiedName:(NSString *)qName{
    
    if ([elementName isEqualToString:xCaretaker]) // end of caretaker
    {
        //NSLog(@"Name: %@\n", caretaker.name);
         // add to array
        [allCaretakers addObject:[[LinkedAccount alloc] initWithEmail:caretaker.email status:caretaker.status name:caretaker.name]];
        
        caretaker.name = @"";
        caretaker.email = @"";
        caretaker.status = 0;
        insideCaretaker = NO;
    }
    else if ([elementName isEqualToString:xName] && insideCaretaker) // exit <name>
    {
        caretaker.name = [currentElementValue copy];
    }
    else if ([elementName isEqualToString:xEmail] && insideCaretaker) // exit <email>
    {
        caretaker.email = [currentElementValue copy];
    }
    else if ([elementName isEqualToString:xstatus] && insideCaretaker) // exit <status>
    {
        caretaker.status = atoi([currentElementValue UTF8String]); 
    }
    
    [currentElementValue setString:@""];
    current_state = blank;
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (current_state != blank)
    {
        [currentElementValue appendString:string];
    }
    
    [xml appendString:string];
}

@end
